home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 9 / Engine / BoundingVolume.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-01  |  7.2 KB  |  162 lines

  1. //-----------------------------------------------------------------------------
  2. // BoundingVolume.h implementation.
  3. // Refer to the BoundingVolume.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // The bounding volume class constructor.
  12. //-----------------------------------------------------------------------------
  13. BoundingVolume::BoundingVolume()
  14. {
  15.     m_box = new BoundingBox;
  16.     m_sphere = new BoundingSphere;
  17.  
  18.     m_ellipsoidRadius = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  19. }
  20.  
  21. //-----------------------------------------------------------------------------
  22. // The bounding volume class destructor.
  23. //-----------------------------------------------------------------------------
  24. BoundingVolume::~BoundingVolume()
  25. {
  26.     SAFE_DELETE( m_box );
  27.     SAFE_DELETE( m_sphere );
  28. }
  29.  
  30. //-----------------------------------------------------------------------------
  31. // Builds a bounding volume to enclose the given mesh.
  32. //-----------------------------------------------------------------------------
  33. void BoundingVolume::BoundingVolumeFromMesh( ID3DXMesh *mesh, D3DXVECTOR3 ellipsoidRadius )
  34. {
  35.     D3DXVECTOR3 *vertices;
  36.     if( SUCCEEDED( mesh->LockVertexBuffer( D3DLOCK_READONLY, (void**)&vertices ) ) )
  37.     {
  38.         D3DXComputeBoundingBox( vertices, mesh->GetNumVertices(), D3DXGetFVFVertexSize( mesh->GetFVF() ), &m_box->min, &m_box->max );
  39.         D3DXComputeBoundingSphere( vertices, mesh->GetNumVertices(), D3DXGetFVFVertexSize( mesh->GetFVF() ), &m_sphere->centre, &m_sphere->radius );
  40.         mesh->UnlockVertexBuffer();
  41.     }
  42.  
  43.     m_sphere->centre.x = m_box->min.x + ( ( m_box->max.x - m_box->min.x ) / 2.0f );
  44.     m_sphere->centre.y = m_box->min.y + ( ( m_box->max.y - m_box->min.y ) / 2.0f );
  45.     m_sphere->centre.z = m_box->min.z + ( ( m_box->max.z - m_box->min.z ) / 2.0f );
  46.  
  47.     m_box->halfSize = (float)max( fabs( m_box->max.x ), max( fabs( m_box->max.y ), fabs( m_box->max.z ) ) );
  48.     m_box->halfSize = (float)max( m_box->halfSize, max( fabs( m_box->min.x ), max( fabs( m_box->min.y ), fabs( m_box->min.z ) ) ) );
  49.  
  50.     m_originalMin = m_box->min;
  51.     m_originalMax = m_box->max;
  52.     m_originalCentre = m_sphere->centre;
  53.  
  54.     SetEllipsoidRadius( ellipsoidRadius );
  55. }
  56.  
  57. //-----------------------------------------------------------------------------
  58. // Builds a bounding volume to enclose the given vertices.
  59. //-----------------------------------------------------------------------------
  60. void BoundingVolume::BoundingVolumeFromVertices( D3DXVECTOR3 *vertices, unsigned long totalVertices, unsigned long vertexStride, D3DXVECTOR3 ellipsoidRadius )
  61. {
  62.     D3DXComputeBoundingBox( vertices, totalVertices, vertexStride, &m_box->min, &m_box->max );
  63.     D3DXComputeBoundingSphere( vertices, totalVertices, vertexStride, &m_sphere->centre, &m_sphere->radius );
  64.  
  65.     m_sphere->centre.x = m_box->min.x + ( ( m_box->max.x - m_box->min.x ) / 2.0f );
  66.     m_sphere->centre.y = m_box->min.y + ( ( m_box->max.y - m_box->min.y ) / 2.0f );
  67.     m_sphere->centre.z = m_box->min.z + ( ( m_box->max.z - m_box->min.z ) / 2.0f );
  68.  
  69.     m_box->halfSize = (float)max( fabs( m_box->max.x ), max( fabs( m_box->max.y ), fabs( m_box->max.z ) ) );
  70.     m_box->halfSize = (float)max( m_box->halfSize, max( fabs( m_box->min.x ), max( fabs( m_box->min.y ), fabs( m_box->min.z ) ) ) );
  71.  
  72.     m_originalMin = m_box->min;
  73.     m_originalMax = m_box->max;
  74.     m_originalCentre = m_sphere->centre;
  75.  
  76.     SetEllipsoidRadius( ellipsoidRadius );
  77. }
  78.  
  79. //-----------------------------------------------------------------------------
  80. // Builds a bounding volume from the given bounding volume details.
  81. //-----------------------------------------------------------------------------
  82. void BoundingVolume::CloneBoundingVolume( BoundingBox *box, BoundingSphere *sphere, D3DXVECTOR3 ellipsoidRadius )
  83. {
  84.     m_box->min = box->min;
  85.     m_box->max = box->max;
  86.     m_sphere->centre = sphere->centre;
  87.     m_sphere->radius = sphere->radius;
  88.  
  89.     m_box->halfSize = (float)max( fabs( m_box->max.x ), max( fabs( m_box->max.y ), fabs( m_box->max.z ) ) );
  90.     m_box->halfSize = (float)max( m_box->halfSize, max( fabs( m_box->min.x ), max( fabs( m_box->min.y ), fabs( m_box->min.z ) ) ) );
  91.  
  92.     m_originalMin = m_box->min;
  93.     m_originalMax = m_box->max;
  94.     m_originalCentre = m_sphere->centre;
  95.  
  96.     SetEllipsoidRadius( ellipsoidRadius );
  97. }
  98.  
  99. //-----------------------------------------------------------------------------
  100. // Repositions the bounding volume by the given matrix.
  101. //-----------------------------------------------------------------------------
  102. void BoundingVolume::RepositionBoundingVolume( D3DXMATRIX *location )
  103. {
  104.     D3DXVec3TransformCoord( &m_box->min, &m_originalMin, location );
  105.     D3DXVec3TransformCoord( &m_box->max, &m_originalMax, location );
  106.     D3DXVec3TransformCoord( &m_sphere->centre, &m_originalCentre, location );
  107. }
  108.  
  109. //-----------------------------------------------------------------------------
  110. // Sets the bounding box's properties.
  111. //-----------------------------------------------------------------------------
  112. void BoundingVolume::SetBoundingBox( D3DXVECTOR3 min, D3DXVECTOR3 max )
  113. {
  114.     m_originalMin = m_box->min = min;
  115.     m_originalMax = m_box->max = max;
  116.  
  117.     m_box->halfSize = (float)max( fabs( m_box->max.x ), max( fabs( m_box->max.y ), fabs( m_box->max.z ) ) );
  118.     m_box->halfSize = (float)max( m_box->halfSize, max( fabs( m_box->min.x ), max( fabs( m_box->min.y ), fabs( m_box->min.z ) ) ) );
  119. }
  120.  
  121. //-----------------------------------------------------------------------------
  122. // Returns the bounding box.
  123. //-----------------------------------------------------------------------------
  124. BoundingBox *BoundingVolume::GetBoundingBox()
  125. {
  126.     return m_box;
  127. }
  128.  
  129. //-----------------------------------------------------------------------------
  130. // Sets the bounding sphere's properties.
  131. //-----------------------------------------------------------------------------
  132. void BoundingVolume::SetBoundingSphere( D3DXVECTOR3 centre, float radius, D3DXVECTOR3 ellipsoidRadius )
  133. {
  134.     m_originalCentre = m_sphere->centre = centre;
  135.     m_sphere->radius = radius;
  136.  
  137.     SetEllipsoidRadius( ellipsoidRadius );
  138. }
  139.  
  140. //-----------------------------------------------------------------------------
  141. // Returns the bounding sphere.
  142. //-----------------------------------------------------------------------------
  143. BoundingSphere *BoundingVolume::GetBoundingSphere()
  144. {
  145.     return m_sphere;
  146. }
  147.  
  148. //-----------------------------------------------------------------------------
  149. // Sets the ellipsoid radius to a percentage of the sphere radius.
  150. //-----------------------------------------------------------------------------
  151. void BoundingVolume::SetEllipsoidRadius( D3DXVECTOR3 ellipsoidRadius )
  152. {
  153.     m_ellipsoidRadius = D3DXVECTOR3( m_sphere->radius * ellipsoidRadius.x, m_sphere->radius * ellipsoidRadius.y, m_sphere->radius * ellipsoidRadius.z );
  154. }
  155.  
  156. //-----------------------------------------------------------------------------
  157. // Returns the ellipsoid radius.
  158. //-----------------------------------------------------------------------------
  159. D3DXVECTOR3 BoundingVolume::GetEllipsoidRadius()
  160. {
  161.     return m_ellipsoidRadius;
  162. }